<?php
require_once ('..' . DIRECTORY_SEPARATOR . 'initialize.php');
include_once(Utils::getPath(array('shared', 'template.php')));
include_once(Utils::getPath(array('repositories', 'solutionRepository.php')));
include_once(Utils::getPath(array('repositories', 'challengeRepository.php')));
include_once(Utils::getPath(array('repositories', 'userRepository.php')));

$template = new Template('application');
$template->initialize();

$challengeRep = new ChallengeRepository();
$challenge = $challengeRep->getById($_GET['id']);

if ($challenge == null) {
    Utils::goToErrorPage();
}

$title = $challenge->getTitle();
$description = $challenge->getDescription();
$filepath = $challenge->getFilePath();
$commentType = 1;
$id = $challenge->getId();

$userRep = new UserRepository();
$user = $userRep->getById($challenge->getIdAuthor());
$filterUrlParam = '&amp;id=' . $id;

$users = $challengeRep->getUsersWhoSolvedChallenge($id);

$user_id = $_SESSION['authenticatedId'];
$user_solved = (int) in_array(array('idauthor' => $user_id, 0 => $user_id), $users);
$user_is_challenge_author = (int) ($user_id == $challenge->getIdAuthor());

$solutionRep = new SolutionRepository();
$solutions = $solutionRep->getSolutionsWithAuthorName($id);
?>

<div id = "item-view">
    <div id = "item-header">
        <div id = "item-title">
<?php echo $title; ?>
        </div>
        <div id = "item-author">
            [made by <a href="<?php echo Utils::getUrl('user/profile.php?id=' . $user->getId()) ?>"><?php echo $user->getUserName() ?></a>]
        </div>
        <div id = "item-info">
<?php
if ($user_solved) {
    ?>
                <span class = "done">Done</span>
                <?php
            } else {
                ?>
                <span class = "not-done">Not done</span>
                <?php
            }
            ?>
            <span class = "done-by">[<?php echo count($users); ?>]</span>
        </div>

    </div>
    <div id = "item-description">
<?php echo $description; ?>
    </div>
    <div id = "item-file">
<?php
if ($filepath != null && count($filepath) > 0) {
    ?>
            <a href="<?php echo Utils::getUrl($filepath) ?>">Download</a> the additional file for this challenge.
            <?php
        } else {
            ?>
            <p>There is no additional file for this challenge.</p>
            <?php
        }
        ?>
    </div>
</div>

<h3>Solutions</h3>
<p>
    <a href="<?php echo Utils::getUrl("solution/new.php?id=$id") ?>">Upload solution!</a>
</p>
<?php
if (count($solutions) == 0) {
    ?>
    <p>There are no solutions for this challenge yet.</p>
    <?php
} else {
    ?>
    
        <table id="list">
            <tr>
                <th>Username</th>
                <th>Date</th>
                <th>Quality</th>
                <th>Valid</th>
                <th>View solution</th>
            </tr>
    <?php
    foreach ($solutions as $solution) {
        $allowed = $user_solved || $user_is_challenge_author || $user_id == $solution['userid'];
        if($allowed) {
            $urlSolution = Utils::getUrl('solution/view.php?id=' . $solution['solutionid']);
        ?>
                <tr class="list-item" onclick="location.href='<?php echo $urlSolution; ?>'">
                    <?php }else{ ?>
                <tr class="list-item">                    
                    <?php } ?>
                    <td><a href="<?php echo Utils::getUrl('user/profile.php?id=' . $solution['userid']) ?>"><?php echo $solution['username'] ?></a></td>
                    <td><?php echo $solution['date']; ?></td>
                    <td><?php echo $solution['quality']; ?></td>
                    <td><?php echo!isset($solution['valid']) ? "Pending" : ($solution['valid'] ? "Yes" : "No") ?></td>
                <?php
                if ($allowed) {
                    ?>
                        <td><a href="<?php echo $urlSolution; ?>">View</a></td>
            <?php
        } else {
            ?>
                        <td>Not allowed</td>
                        <?php
                    }
                    ?>

                </tr>
                    <?php
                }
                ?>        
        </table>
    
                <?php
            }
            ?>
<div id="comment-list">
        <?php
        include(Utils::getPath(array('comment', 'list.php')));
        ?>
</div>
<input type="hidden" id="url-list" value="<?php echo htmlentities(Utils::getUrl('comment/list.php?id='.$challenge->getId().'&commentType=1')) ?>"/>
<script type="text/javascript" src="<?php echo Utils::getUrl('scripts/comment.js') ?>"></script>
<script type="text/javascript">
    $(document).ready(function(){
            set_pageslink_onclick();        
    });
</script>
<?php
$template->createPage($title);
?>